const { addshopper, findshop, getuserdata, deleteid, updetebox } = require('../../service/shopper/req')
module.exports = {
    async add(ctx, next) {
        const { userinfo, shopinfo, ...data } = JSON.parse(ctx.request.body.data)
        const img = ctx.request.files.img.newFilename
        const rudata = await addshopper({ ...data, userinfo: JSON.stringify(userinfo), shopinfo: JSON.stringify(shopinfo), img })
        console.log("add")

        ctx.body = {
            code: 200,
            msg: '成功',
            data: rudata
        }

    },
    async getshopper(ctx, next) {
        const userid = ctx.params.id
        const rudata = await findshop(userid)
        console.log("getshopper")
        if (rudata) {
            return ctx.body = {
                code: 200,
                msg: '成功',
                data: rudata
            }
        }
        return ctx.body = {
            code: -1,
            msg: '没有',
        }
    },
    async getuser(ctx, next) {
        let { limit, offset, findkey } = ctx.query
        findkey = JSON.parse(findkey)
        const { rows, count } = await getuserdata(+limit, +offset, findkey)
        ctx.body = {
            code: 200,
            msg: '成功',
            data: {
                count,
                page: Math.ceil(count / limit),
                data: rows
            }
        }
    },
    async deletes(ctx, next) {
        const id = ctx.request.body.id
        const rudata = await deleteid(id)
        if (rudata) {
            return ctx.body = {
                code: 200,
                msg: '删除成功！',
            }
        }
        return ctx.body = {
            code: 200,
            msg: '删除失败！',
        }
    },
    async freeze(ctx, next) {
        const id = ctx.request.body.id
        console.log(id)
        const rudata = await updetebox(id, { kai: 3 })
        if (rudata) {
            return ctx.body = {
                code: 200,
                msg: '冻结成功！',
            }
        }
    },
    async unfreeze(ctx, next) {
        const id = ctx.request.body.id
        const rudata = await updetebox(id, { kai: 2 })
        if (rudata) {
            return ctx.body = {
                code: 200,
                msg: '解冻成功！',
            }
        }
    }
}